﻿package net.wangit.util;

import java.text.DecimalFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CurrencyUtil {

	private static final Log log = LogFactory
			.getLog(net.wangit.util.CurrencyUtil.class);
	private static final String NUMBERS[] = { "零", "壹", "贰", "叁", "肆", "伍",
			"陆", "柒", "捌", "玖" };
	private static final String IUNIT[] = { "元", "拾", "佰", "仟", "万", "拾", "佰",
			"仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };
	private static final String DUNIT[] = { "角", "分", "厘" };
	private static int LIMIT = 16;

	public CurrencyUtil() {
	}

	public static String format(String str) {
		if (isCurrency(str))
			return DecimalFormat.getCurrencyInstance().format(str);
		else
			return str;
	}

	public static boolean isCurrency(String str) {
		return !Checker.isEmpty(str);
	}

	public static String toChinese(String str) {
		String number = getFormatNum(str);
		if (!isMoney(number))
			return number;
		String integerStr;
		String decimalStr;
		if (number.indexOf(".") > 0) {
			integerStr = number.substring(0, number.indexOf("."));
			decimalStr = number.substring(number.indexOf(".") + 1);
		} else if (number.indexOf(".") == 0) {
			integerStr = "";
			decimalStr = number.substring(1);
		} else {
			integerStr = number;
			decimalStr = "";
		}
		if (!integerStr.equals("")) {
			integerStr = Long.toString(Long.parseLong(integerStr));
			if (integerStr.equals("0"))
				integerStr = "";
		}
		if (integerStr.length() > LIMIT) {
			log.error((new StringBuilder(String.valueOf(number))).append(
					":超出处理能力").toString());
			return number;
		} else {
			int integers[] = toArray(integerStr);
			boolean isMust5 = isMust5(integerStr);
			int decimals[] = toArray(decimalStr);
			return (new StringBuilder(String.valueOf(getChineseInteger(
					integers, isMust5)))).append(getChineseDecimal(decimals))
					.toString();
		}
	}

	private static int[] toArray(String number) {
		int array[] = new int[number.length()];
		for (int i = 0; i < number.length(); i++)
			array[i] = Integer.parseInt(number.substring(i, i + 1));

		return array;
	}

	private static String getChineseInteger(int integers[], boolean isMust5) {
		StringBuffer chineseInteger = new StringBuffer("");
		String key = "";
		int length = integers.length;
		for (int ii = 0; ii < length; ii++) {
			if (integers[ii] == 0) {
				if (length - ii == 13)
					key = IUNIT[4];
				else if (length - ii == 9)
					key = IUNIT[8];
				else if (length - ii == 5 && isMust5)
					key = IUNIT[4];
				else if (length - ii == 1)
					key = IUNIT[0];
				if (length - ii > 1 && integers[ii + 1] != 0)
					key = (new StringBuilder(String.valueOf(key))).append(
							NUMBERS[0]).toString();
				else
					key = (new StringBuilder(String.valueOf(key))).toString();
			}
			chineseInteger.append(integers[ii] != 0 ? (new StringBuilder(String
					.valueOf(NUMBERS[integers[ii]]))).append(
					IUNIT[length - ii - 1]).toString() : key);
			key = "";
		}

		return chineseInteger.toString();
	}

	private static String getChineseDecimal(int decimals[]) {
		StringBuffer chineseDecimal = new StringBuffer("");
		for (int ii = 0; ii < decimals.length; ii++) {
			if (ii == 3)
				break;
			chineseDecimal.append(decimals[ii] != 0 ? (new StringBuilder(String
					.valueOf(NUMBERS[decimals[ii]]))).append(DUNIT[ii])
					.toString() : "");
		}

		return chineseDecimal.toString();
	}

	private static String getFormatNum(String number) {
		return StringUtil.replace(number, ",", "");
	}

	private static boolean isMust5(String integerStr) {
		int length = integerStr.length();
		if (length > 4) {
			String subInteger = "";
			if (length > 8)
				subInteger = integerStr.substring(length - 8, length - 4);
			else
				subInteger = integerStr.substring(0, length - 4);
			return Integer.parseInt(subInteger) > 0;
		} else {
			return false;
		}
	}

	private static boolean isMoney(String number) {
		try {
			Double.parseDouble(number);
		} catch (Exception ex) {
			log.error((new StringBuilder(String.valueOf(number))).append(
					"不是合法金额").toString());
			return false;
		}
		return true;
	}

}
